#include "cloud_model_diagnosis.h"

#include <algorithm>
#include <future>
#include <inttypes.h>
#include <stdio.h>
#include <string.h>

#include "base_file_util.h"
#include "base_utils.h"
#include "base_string.h"
#include "base_log.h"
#include "common_shell.h"

#include "cloud_model_audio.h"
#include "cloud_model_image.h"

namespace El {
namespace Cloud {

CloudModelDiagnosis &CloudModelDiagnosis::GetInstance()
{
    static CloudModelDiagnosis instance;
    return instance;
}

bool CloudModelDiagnosis::Start()
{
    Common::Shell::GetInstance().RegisterCommand(
        "cloud", std::bind(&CloudModelDiagnosis::AnalyzeImage, this, std::placeholders::_1));
    return true;
}

void CloudModelDiagnosis::AnalyzeImage(const std::string &args)
{
    std::vector<std::string> vec_args;
    Base::Split(args, " ", vec_args);
    if (vec_args.size() < 3) {
        LOG_ERROR("Invalid args: {}", args.c_str());
        return;
    }

    std::string text = "";
    if (vec_args[0] == "new") {
        int32_t conversation_id = CloudModelImage::GetInstance().NewConversation();
        LOG_INFO("创建新对话: {}", conversation_id);
    } else if (vec_args[0] == "image") {
        int32_t conversation_id = std::stoi(vec_args[1]);
        std::vector<uint8_t> image_buffer;
        Base::LoadBufferFromFile("../resource/1080p.jpg", image_buffer);
        
        CloudModelImage::ImageData image_data{
            image_buffer.data(),
            image_buffer.size(),
            1920,
            1080
        };
        
        text = CloudModelImage::GetInstance().AnalyzeImage(conversation_id, image_data, vec_args[2]);
    } else if (vec_args[0] == "text") {
        int32_t conversation_id = std::stoi(vec_args[1]);
        text = CloudModelImage::GetInstance().GenerateText(conversation_id, vec_args[2]);
    }
    LOG_INFO("生成文本完成: {}", text.c_str());
}

} // namespace Cloud
} // namespace El